﻿/************************************************************************************
 * Copyright (c) 2016 北京大象科技有限公司 All Rights Reserved.
 * CLR版本：4.0.30319.42000
 *公司名称：北京大象科技有限公司
 *命名空间：ET.Framework
 *文件名：  LogHelper
 *版本号：  V1.0.0.0
 *唯一标识：7d7fcd65-859e-4c6a-8083-4bdb9630b3c6
 *创建人：  杜鹏
 *电子邮箱：dupeng@bjdaxiang.cn
 *QQ:       4909004
 *创建时间：2016/3/28 17:15:43
 *描述：    
 *
 *=====================================================================
 *修改标记
 *修改时间：2016/3/28 17:15:43
 *修改人：  杜鹏
 *版本号：  V1.0.0.0
 *描述：
/************************************************************************************/

using log4net.Config;
using System;
using System.IO;
using System.Xml;

namespace ET.Score.Framework
{
    //public enum LogLevel
    //{
    //    Debug = 0,
    //    Information = 1,
    //    Warning = 2,
    //    Error = 3,
    //    Fatal = 4,
    //}
    /// <summary>
    /// 日志记录帮助类
    /// </summary>
    public class LogHelper
    {
        static LogHelper()
        {
            try
            {
                var xml = XmlConfigurator.Configure(File.OpenRead(@"log4net.config.xml"));

                DeleteHisFile();
            }
            catch (IOException)
            {

            }
            //  log4net.LogManager.Initialize(@"log4net.config.xml", 10);
            log4net.ILog _log = log4net.LogManager.GetLogger("log");
        }
        static void DeleteHisFile()
        {
            XmlDocument xd = new XmlDocument();
            string fileName = @"log4net.config.xml";
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.IgnoreComments = true;//忽略文档里面的注释
            XmlReader reader = XmlReader.Create(fileName, settings);
            xd.Load(reader);
            //获取到XML的根元素进行操作
            XmlNode xn = xd.SelectSingleNode("log4net");
            xn = xn.SelectSingleNode("appender");
            XmlNodeList xnl = xn.ChildNodes;
            XmlElement node = null;
            int datanum = 0;

            foreach (XmlNode x in xnl)
            {
                XmlElement xe = (XmlElement)x;
                string name = xe.GetAttribute("name").ToString();
                if (name == "MaxSizeRollBackups")
                {
                    datanum = Convert.ToInt32(xe.GetAttribute("value").ToString());
                    break;
                }
            }
            string ss = System.IO.Directory.GetCurrentDirectory();


            DirectoryInfo dir = new DirectoryInfo(ss);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
            DeleteLogs(ss + "\\Log\\Info", datanum);
            DeleteLogs(ss + "\\Log\\Error", datanum);


        }
        public static void DeleteLogs(string filrdir, int datanum)
        {

            DirectoryInfo dir = new DirectoryInfo(filrdir);
            FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
            string zerotime = DateTime.Now.ToString("yyyy-MM-dd 00:00:00");
            DateTime dt = Convert.ToDateTime(zerotime);
            dt = dt.AddDays(-1 * datanum);
            foreach (FileSystemInfo i in fileinfo)
            {
                if (i is DirectoryInfo)            //判断是否文件夹
                {
                    DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                    subdir.Delete(true);          //删除子目录和文件
                }
                else
                {
                    DateTime ct = i.LastWriteTime;
                    if (ct.Ticks < dt.Ticks)
                        File.Delete(i.FullName);      //删除指定文件
                }
            }
        }
        /// <summary>
        /// 写普通信息
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public static void WriteInfo(Type t, string msg)
        {
            try
            {
                log4net.ILog log = log4net.LogManager.GetLogger(t);
                log.Info(msg);
            }
            catch { }
        }

        /// <summary>
        /// 写调试信息
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public static void WriteDebug(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Debug(msg);
        }

        /// <summary>
        /// 写警告信息
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public static void WriteWarn(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Warn(msg);
        }

        /// <summary>
        /// 写普通错误信息
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public static void WriteError(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error(msg);
        }

        /// <summary>
        /// 写严重错误信息
        /// </summary>
        /// <param name="t"></param>
        /// <param name="msg"></param>
        public static void WriteFatal(Type t, string msg)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(t);
            log.Error(msg);
        }
    }
}
